package org.wangzs.blog.action.blog;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.jsoup.Jsoup;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.wangzs.blog.entity.Page;
import org.wangzs.blog.entity.TPost;
import org.wangzs.blog.service.inner.IPostService;

@Controller
public class PostAction {
	
	private static final int PAGESIZE = 3;
	
	@Resource(name = "PostServiceImpl")
	private IPostService postService;
	
	@RequestMapping("/")
	public String index() {
		return "forward:/posts/1";
	}
	
	@RequestMapping("/posts/{p}")
	public String list(@PathVariable(value="p")int pageNum, Model model) {
		Page<TPost> page =  postService.get(PAGESIZE, pageNum);
		List<TPost> postList = page.getResult();
		List<Map<String, Object>> listMap = new ArrayList<Map<String, Object>>();
		for(TPost post : postList) {
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			post.getCreatedTime();
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("id", post.getId());
			map.put("title", post.getTitle());
			String content = Jsoup.parse(post.getContent()).text();
			if(content.length()>180){
				content = content.substring(0, 180) + "...";
			}
			map.put("content", content);
			map.put("createdTime", format.format(post.getCreatedTime()));
			map.put("commentNum", 3);
			map.put("readNum", 17);
			listMap.add(map);
			model.addAttribute("listMap", listMap);
		}
		model.addAttribute("page", page);
		
		return "blog/post-list";
	}
	
	@RequestMapping(value="/post/{id}", method=RequestMethod.GET)
	public String detail(@PathVariable(value="id") String id, Model model) {
		
		TPost post = postService.get(id);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", post.getId());
		map.put("title", post.getTitle());
		map.put("content", post.getContent());
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		map.put("createdTime", format.format(post.getCreatedTime()));
		map.put("commentNum", 3);
		map.put("readNum", 17);
		model.addAttribute("map", map);
		return "blog/post-detail";
	}
	
	@RequestMapping(value="/admin/post", method=RequestMethod.GET)
	public String input() {
		return "blog/post-input";
	}
	
	@RequestMapping(value="/admin/post", method=RequestMethod.POST)
	public String save(@RequestParam String id, @RequestParam String title,
			@RequestParam String content,
			@CookieValue(value="username", required=false) String username,
			@CookieValue(value="password", required=false) String password) {
		boolean login = "e241138".equals(username) && "BBbb138717".equals(password);
		if(!login) {
			return "redirect:/login";
		}
		if("".equals(id)){
			TPost post = new TPost();
			post.setTitle(title);
			post.setContent(content);
			postService.save(post);
		}else{
			TPost post = postService.get(id);
			post.setTitle(title);
			post.setContent(content);
			postService.update(post);
		}
		return "redirect:/posts/1";
	}
	
	@RequestMapping(value="/admin/post/{id}", method=RequestMethod.GET)
	public String edit(@PathVariable(value="id") String id, Model model) {
		TPost post = postService.get(id);
		model.addAttribute("id", post.getId());
		model.addAttribute("title", post.getTitle());
		model.addAttribute("content", post.getContent());
		return "blog/post-input";
	}
	
	@RequestMapping(value="/admin/post/{id}", method=RequestMethod.DELETE)
	public String delete(@RequestParam(value="id", required=true) String id){
		postService.delete(id);
		return "list";
	}
	
	@RequestMapping(value="/demo", method=RequestMethod.GET)
	public String demo(){
		return "blog/demo";
	}
}
